<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">







  <link rel="alternate" href="/atom.xml" title="最好的安排" type="application/atom+xml" />






<meta name="description" content="web应用集群入门-利用docker在单机搭建web应用集群实现session共享">
<meta name="keywords" content="docker使用,nginx集群搭建,反向代理,负载均衡怎么搭建,怎么实现PHPsession共享,容器编排">
<meta property="og:type" content="article">
<meta property="og:title" content="docker搭建nginx服务器集群">
<meta property="og:url" content="https://blog.wm07.cn/2017/docker-nginx-slb.html">
<meta property="og:site_name" content="最好的安排">
<meta property="og:description" content="web应用集群入门-利用docker在单机搭建web应用集群实现session共享">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://cdn.wm07.cn/docker-compose-version.png">
<meta property="og:image" content="https://cdn.wm07.cn/docker-compose-ps.png">
<meta property="og:image" content="https://cdn.wm07.cn/slb-web2.png">
<meta property="og:image" content="https://cdn.wm07.cn/slb-web3.png">
<meta property="og:image" content="https://cdn.wm07.cn/slb-web4.png">
<meta property="og:image" content="https://cdn.wm07.cn/add-redis-images.png">
<meta property="og:image" content="https://cdn.wm07.cn/docker-compose-stop-rm.png">
<meta property="og:image" content="https://cdn.wm07.cn/docker-slb-phpinfo.png">
<meta property="og:image" content="https://cdn.wm07.cn/docker-slb-session1.png">
<meta property="og:image" content="https://cdn.wm07.cn/docker-slb-session2.png">
<meta property="og:image" content="https://cdn.wm07.cn/docker-slb-session3.png">
<meta property="og:updated_time" content="2017-12-04T00:48:04.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="docker搭建nginx服务器集群">
<meta name="twitter:description" content="web应用集群入门-利用docker在单机搭建web应用集群实现session共享">
<meta name="twitter:image" content="https://cdn.wm07.cn/docker-compose-version.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://blog.wm07.cn/2017/docker-nginx-slb.html"/>





  <title>docker搭建nginx服务器集群 | 最好的安排</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">最好的安排</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">深度</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://blog.wm07.cn/2017/docker-nginx-slb.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="空城">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://cdn.wm07.cn/pic.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="最好的安排">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">docker搭建nginx服务器集群</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-01T16:09:34+08:00">
                2017-12-01
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/docker-nginx-slb.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2017/docker-nginx-slb.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2017/docker-nginx-slb.html" class="leancloud_visitors" data-flag-title="docker搭建nginx服务器集群">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          
              <div class="post-description">
                  web应用集群入门-利用docker在单机搭建web应用集群实现session共享
              </div>
          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>环境要求:安装了docker的主机 (本文示例环境为centos7.4)  </p>
<h3 id="准备镜像"><a href="#准备镜像" class="headerlink" title="准备镜像"></a>准备镜像</h3><p>首先把所有需要用到的镜像拉取下来</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># nginx</span></span><br><span class="line">$ docker pull nginx</span><br><span class="line"></span><br><span class="line"><span class="comment"># php &amp; php-fpm</span></span><br><span class="line">$ docker pull php:7.1.0-fpm</span><br><span class="line"></span><br><span class="line"><span class="comment"># redis</span></span><br><span class="line">$ docker pull redis</span><br></pre></td></tr></table></figure>
<h3 id="docker-compose容器编排"><a href="#docker-compose容器编排" class="headerlink" title="docker-compose容器编排"></a>docker-compose容器编排</h3><p>Docker Compose 是 Docker 容器进行编排的工具，定义和运行多容器的应用，可以一条命令启动多个容器。</p>
<h4 id="安装Compose"><a href="#安装Compose" class="headerlink" title="安装Compose"></a>安装Compose</h4><p>直接从github下载即可，前提要先安装Docker，版本要1.9.1以上<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` &gt; /usr/<span class="built_in">local</span>/bin/docker-compose</span><br><span class="line">$ chmod +x /usr/<span class="built_in">local</span>/bin/docker-compose</span><br></pre></td></tr></table></figure></p>
<p>安装后确认:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ docker-compose --version</span><br></pre></td></tr></table></figure></p>
<p><img src="https://cdn.wm07.cn/docker-compose-version.png" alt="Compose-version">  </p>
<h3 id="使用Compose启动多个Nginx容器"><a href="#使用Compose启动多个Nginx容器" class="headerlink" title="使用Compose启动多个Nginx容器"></a>使用Compose启动多个Nginx容器</h3><h4 id="创建编排脚本"><a href="#创建编排脚本" class="headerlink" title="创建编排脚本"></a>创建编排脚本</h4><p>在 /docker/nginx 目录下创建 docker-compose.yml<br><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">version:</span> <span class="string">'2'</span></span><br><span class="line"><span class="attr">services:</span></span><br><span class="line"><span class="attr">        web1:</span></span><br><span class="line"><span class="attr">          image:</span> <span class="string">nginx</span></span><br><span class="line"><span class="attr">          links:</span> </span><br><span class="line"><span class="bullet">            -</span> <span class="string">web2</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">web3</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">web4</span></span><br><span class="line"><span class="attr">          ports:</span> </span><br><span class="line"><span class="bullet">            -</span> <span class="string">"8080:8080"</span></span><br><span class="line"><span class="attr">          volumes:</span> </span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/nginx/web1/conf.d:/etc/nginx/conf.d"</span></span><br><span class="line"></span><br><span class="line"><span class="attr">        web2:</span> </span><br><span class="line"><span class="attr">          image:</span> <span class="string">nginx</span></span><br><span class="line"><span class="attr">          volumes:</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/slb/www2:/www"</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/nginx/web2/conf.d:/etc/nginx/conf.d"</span></span><br><span class="line"></span><br><span class="line"><span class="attr">        web3:</span> </span><br><span class="line"><span class="attr">          image:</span> <span class="string">nginx</span></span><br><span class="line"><span class="attr">          volumes:</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/slb/www3:/www"</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/nginx/web3/conf.d:/etc/nginx/conf.d"</span></span><br><span class="line"></span><br><span class="line"><span class="attr">        web4:</span> </span><br><span class="line"><span class="attr">          image:</span> <span class="string">nginx</span></span><br><span class="line"><span class="attr">          volumes:</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/slb/www4:/www"</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/nginx/web4/conf.d:/etc/nginx/conf.d"</span></span><br></pre></td></tr></table></figure></p>
<p>启动容器<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># up:创建并启动 -d:后台运行</span></span><br><span class="line">$ docker-compose up -d</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看容器</span></span><br><span class="line">$ docker-compose ps</span><br></pre></td></tr></table></figure></p>
<p><img src="https://cdn.wm07.cn/docker-compose-ps.png" alt="docker-compose-ps"></p>
<h3 id="配置Nginx"><a href="#配置Nginx" class="headerlink" title="配置Nginx"></a>配置Nginx</h3><h4 id="反向代理Nginx配置"><a href="#反向代理Nginx配置" class="headerlink" title="反向代理Nginx配置"></a>反向代理Nginx配置</h4><p>添加upstream节点,默认使用轮询方式进行负载<br>编辑并保存 /docker/nginx/web1/conf.d/slb.conf<br><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">upstream</span> slb &#123;</span><br><span class="line">   <span class="attribute">server</span> web2;</span><br><span class="line">   <span class="attribute">server</span> web3;</span><br><span class="line">   <span class="attribute">server</span> web4;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="section">server</span> &#123;</span><br><span class="line">   <span class="attribute">listen</span> <span class="number">8080</span>;</span><br><span class="line">   <span class="attribute">server_name</span> <span class="number">127.0.0.1</span>;</span><br><span class="line"></span><br><span class="line">   <span class="attribute">gzip</span> <span class="literal">on</span>;</span><br><span class="line">   <span class="attribute">location</span> /&#123;</span><br><span class="line">      <span class="attribute">root</span> /www;</span><br><span class="line">      <span class="attribute">index</span> index.html index.php;</span><br><span class="line"></span><br><span class="line">      <span class="comment"># 反向代理的主机头</span></span><br><span class="line">      <span class="attribute">proxy_pass</span>  http://slb;</span><br><span class="line">      <span class="attribute">proxy_set_header</span> Host   <span class="variable">$host</span>;</span><br><span class="line">      <span class="attribute">proxy_set_header</span>   X-Real-IP        <span class="variable">$remote_addr</span>;</span><br><span class="line">      <span class="attribute">proxy_set_header</span>   X-Forwarded-For  <span class="variable">$proxy_add_x_forwarded_for</span>;</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="web应用Nginx配置"><a href="#web应用Nginx配置" class="headerlink" title="web应用Nginx配置"></a>web应用Nginx配置</h4><p>编辑并保存 /docker/nginx/web2/conf.d/demo.conf (其余的web3与web4同理)<br><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">server</span> &#123;</span><br><span class="line">  <span class="attribute">listen</span>  <span class="number">80</span>;</span><br><span class="line">  <span class="attribute">server_name</span> localhost;</span><br><span class="line">  <span class="attribute">gzip</span> <span class="literal">on</span>;</span><br><span class="line">  <span class="attribute">location</span> / &#123;</span><br><span class="line">    <span class="attribute">root</span>   /www;</span><br><span class="line">    <span class="attribute">index</span> index.html index.htm;</span><br><span class="line">    <span class="attribute">autoindex</span> <span class="literal">off</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="标记Nginx目录"><a href="#标记Nginx目录" class="headerlink" title="标记Nginx目录"></a>标记Nginx目录</h4><p>为了确定Nginx集群是否搭建成功,分别在 web2 web3 web4 的主目录放置不同的html文档<br>编辑并保存 /docker/nginx/slb/web2/index.html (其余的web3与web4同理)<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 不同目录写上不同的标记 --&gt;</span></span><br><span class="line">  web2</span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>浏览器访问 127.0.0.1:8080 并刷新<br><img src="https://cdn.wm07.cn/slb-web2.png" alt="slb-web2"><br><img src="https://cdn.wm07.cn/slb-web3.png" alt="slb-web3"><br><img src="https://cdn.wm07.cn/slb-web4.png" alt="slb-web4"></p>
<p>至此,Nginx集群已搭建完成!</p>
<h3 id="PHP环境安装"><a href="#PHP环境安装" class="headerlink" title="PHP环境安装"></a>PHP环境安装</h3><p>在准备镜像的时候拉了一个php:7.1.0-fpm的镜像,但为了实现session共享,解决集群之间session问题,需要在此镜像改动一下,安装redis拓展,修改php的session储存方式为redis<br>在 /docker/php/ 目录下创建Dockerfile文件<br><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">FROM</span> php:<span class="number">7.1</span>.<span class="number">0</span>-fpm</span><br><span class="line"></span><br><span class="line"><span class="keyword">MAINTAINER</span> xiaowei <span class="string">"694623056@qq.com"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Install php extension--redis</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> <span class="built_in">cd</span> /usr/<span class="built_in">local</span>/ \</span></span><br><span class="line"><span class="bash">  &amp;&amp; mkdir redis_phpext \</span></span><br><span class="line"><span class="bash">  &amp;&amp; curl -o redis_phpext/redis-3.1.3.tgz http://101.96.10.64/pecl.php.net/get/redis-3.1.3.tgz \</span></span><br><span class="line"><span class="bash">  &amp;&amp; <span class="built_in">cd</span> redis_phpext \</span></span><br><span class="line"><span class="bash">  &amp;&amp; tar zxvf redis-3.1.3.tgz \</span></span><br><span class="line"><span class="bash">  &amp;&amp; <span class="built_in">cd</span> redis-3.1.3 \</span></span><br><span class="line"><span class="bash">  &amp;&amp; phpize \</span></span><br><span class="line"><span class="bash">  &amp;&amp; ./configure --with-php-config=/usr/<span class="built_in">local</span>/bin/php-config \</span></span><br><span class="line"><span class="bash">  &amp;&amp; make &amp;&amp; make install \</span></span><br><span class="line"><span class="bash">  &amp;&amp; rm -rf /usr/<span class="built_in">local</span>/redis_phpext</span></span><br></pre></td></tr></table></figure></p>
<h4 id="创建自定义镜像"><a href="#创建自定义镜像" class="headerlink" title="创建自定义镜像"></a>创建自定义镜像</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 切换目录</span></span><br><span class="line">$ <span class="built_in">cd</span> /docker/php</span><br><span class="line"><span class="comment"># build: 创建,切记勿忽略后面的 . 意为当前目录</span></span><br><span class="line">$ docker build -t php:add_redis .</span><br></pre></td></tr></table></figure>
<p>创建完成后 docker images 查看是否成功<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ docker images</span><br></pre></td></tr></table></figure></p>
<p><img src="https://cdn.wm07.cn/add-redis-images.png" alt="add-redis-images"></p>
<h3 id="容器依赖"><a href="#容器依赖" class="headerlink" title="容器依赖"></a>容器依赖</h3><p>修改docker-compose.yml文件,添加php与redis的编排,这里已经不需要测试集群,可以把Nginx应用网站根目录统一挂载到 /docker/www</p>
<h4 id="最终编排脚本"><a href="#最终编排脚本" class="headerlink" title="最终编排脚本"></a>最终编排脚本</h4><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">version:</span> <span class="string">'2'</span></span><br><span class="line"><span class="attr">services:</span></span><br><span class="line"><span class="attr">        web1:</span></span><br><span class="line"><span class="attr">          image:</span> <span class="string">nginx</span></span><br><span class="line"><span class="attr">          links:</span> </span><br><span class="line"><span class="bullet">            -</span> <span class="string">web2</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">web3</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">web4</span></span><br><span class="line"><span class="attr">          ports:</span> </span><br><span class="line"><span class="bullet">            -</span> <span class="string">"8080:8080"</span></span><br><span class="line"><span class="attr">          volumes:</span> </span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/nginx/web1/conf.d:/etc/nginx/conf.d"</span></span><br><span class="line"></span><br><span class="line"><span class="attr">        web2:</span> </span><br><span class="line"><span class="attr">          image:</span> <span class="string">nginx</span></span><br><span class="line"><span class="attr">          links:</span> </span><br><span class="line"><span class="bullet">            -</span> <span class="string">php</span></span><br><span class="line"><span class="attr">          volumes:</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/www:/www"</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/nginx/web2/conf.d:/etc/nginx/conf.d"</span></span><br><span class="line"></span><br><span class="line"><span class="attr">        web3:</span> </span><br><span class="line"><span class="attr">          image:</span> <span class="string">nginx</span></span><br><span class="line"><span class="attr">          links:</span> </span><br><span class="line"><span class="bullet">            -</span> <span class="string">php</span></span><br><span class="line"><span class="attr">          volumes:</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/www:/www"</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/nginx/web3/conf.d:/etc/nginx/conf.d"</span></span><br><span class="line"></span><br><span class="line"><span class="attr">        web4:</span> </span><br><span class="line"><span class="attr">          image:</span> <span class="string">nginx</span></span><br><span class="line"><span class="attr">          links:</span> </span><br><span class="line"><span class="bullet">            -</span> <span class="string">php</span></span><br><span class="line"><span class="attr">          volumes:</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/www:/www"</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/nginx/web4/conf.d:/etc/nginx/conf.d"</span></span><br><span class="line"><span class="attr">        php:</span> </span><br><span class="line"><span class="attr">          build:</span> <span class="string">/docker/php/</span></span><br><span class="line"><span class="attr">          links:</span> </span><br><span class="line"><span class="bullet">            -</span> <span class="string">redis</span></span><br><span class="line"><span class="attr">          volumes:</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/www:/www"</span></span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/php/conf.d:/usr/local/etc/php/conf.d"</span></span><br><span class="line"></span><br><span class="line"><span class="attr">        redis:</span></span><br><span class="line"><span class="attr">          image:</span> <span class="string">redis</span></span><br><span class="line"><span class="attr">          volumes:</span> </span><br><span class="line"><span class="bullet">            -</span> <span class="string">"/docker/redis/data:/data"</span></span><br><span class="line"><span class="attr">          ports:</span> </span><br><span class="line"><span class="bullet">            -</span> <span class="string">"6379:6379"</span></span><br><span class="line">          <span class="string">command</span> <span class="string">:</span> <span class="string">redis-server</span> <span class="bullet">--appendonly</span> <span class="literal">yes</span></span><br></pre></td></tr></table></figure>
<h4 id="添加php配置文件"><a href="#添加php配置文件" class="headerlink" title="添加php配置文件"></a>添加php配置文件</h4><p>在 /docker/php/conf.d/ 目录下创建文件redis.ini<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">;添加redis拓展</span></span><br><span class="line"><span class="attr">extension</span> = redis.so</span><br><span class="line"></span><br><span class="line"><span class="comment">;设置session储存方式为redis</span></span><br><span class="line">session.save_handler = redis</span><br><span class="line">session.save_path = redis:6379</span><br></pre></td></tr></table></figure></p>
<h4 id="修改web应用Nginx配置"><a href="#修改web应用Nginx配置" class="headerlink" title="修改web应用Nginx配置"></a>修改web应用Nginx配置</h4><p>将php类型文件转发给php-fpm处理,修改 web2 web3 web4 Nginx配置文件<br>/docker/nginx/web[2-3]/demo.conf<br><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">server</span> &#123;</span><br><span class="line">  <span class="attribute">listen</span>  <span class="number">80</span>;</span><br><span class="line">  <span class="attribute">server_name</span> localhost;</span><br><span class="line">  <span class="attribute">gzip</span> <span class="literal">on</span>;</span><br><span class="line">  <span class="attribute">location</span> / &#123;</span><br><span class="line">    <span class="attribute">root</span>   /www;</span><br><span class="line">    <span class="attribute">index</span> index.php index.html index.htm;</span><br><span class="line">    <span class="attribute">autoindex</span> <span class="literal">off</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="attribute">location</span> <span class="regexp">~ \.php</span> &#123;</span><br><span class="line">   <span class="attribute">root</span>  /www;</span><br><span class="line">   <span class="attribute">fastcgi_pass</span> php:<span class="number">9000</span>;</span><br><span class="line">   <span class="attribute">fastcgi_index</span> index.php;</span><br><span class="line">   <span class="attribute">fastcgi_split_path_info</span><span class="regexp"> ^((?U).+\.php)(/?.+)$</span>;</span><br><span class="line">   <span class="attribute">fastcgi_param</span> SCRIPT_FILENAME <span class="variable">$document_root</span><span class="variable">$fastcgi_script_name</span>;</span><br><span class="line">   <span class="attribute">fastcgi_param</span> PATH_INFO <span class="variable">$fastcgi_path_info</span>;</span><br><span class="line">   <span class="attribute">fastcgi_param</span> PATH_TRANSLATED <span class="variable">$document_root</span><span class="variable">$fastcgi_path_info</span>;</span><br><span class="line">   <span class="attribute">include</span>  fastcgi_params;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="重新编排容器"><a href="#重新编排容器" class="headerlink" title="重新编排容器"></a>重新编排容器</h4><p>将一开始编排的集群容器停止并删除,重新创建并启动容器<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 停止</span></span><br><span class="line">$ docker-compose stop</span><br><span class="line"><span class="comment"># 删除 需要确认 y</span></span><br><span class="line">$ docker-compose rm</span><br><span class="line"><span class="comment"># 创建容器启动并后台运行</span></span><br><span class="line">$ docker-compose up -d</span><br></pre></td></tr></table></figure></p>
<p><img src="https://cdn.wm07.cn/docker-compose-stop-rm.png" alt="docker-compose-stop-rm"></p>
<h3 id="测试环境"><a href="#测试环境" class="headerlink" title="测试环境!"></a>测试环境!</h3><h4 id="phpinfo"><a href="#phpinfo" class="headerlink" title="phpinfo"></a>phpinfo</h4><p>在 /docker/www 目录创建 index.php文件<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">echo</span> phpinfo();</span><br></pre></td></tr></table></figure></p>
<p>浏览器访问 127.0.0.1:8080/index.php<br><img src="https://cdn.wm07.cn/docker-slb-phpinfo.png" alt="docker-slb-phpinfo"></p>
<h4 id="session共享"><a href="#session共享" class="headerlink" title="session共享"></a>session共享</h4><p>在 /docker/www 目录创建 session.php文件<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line">session_start();</span><br><span class="line"><span class="keyword">echo</span> <span class="string">'SESSION_ID:'</span>.session_id();</span><br></pre></td></tr></table></figure></p>
<p>浏览器访问 127.0.0.1:8080/session.php 每次的session_id相同则表明已实现session共享<br><img src="https://cdn.wm07.cn/docker-slb-session1.png" alt="docker-slb-session"><br><img src="https://cdn.wm07.cn/docker-slb-session2.png" alt="docker-slb-session"><br><img src="https://cdn.wm07.cn/docker-slb-session3.png" alt="docker-slb-session">  </p>
<blockquote>
<p>本文出自个人博客 <strong><a href="https://blog.wm07.cn/2017/docker-nginx-slb.html">最好的安排</a></strong> 转载请注明出处!</p>
</blockquote>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/docker/" rel="tag"># docker</a>
          
            <a href="/tags/Nginx/" rel="tag"># Nginx</a>
          
            <a href="/tags/负载均衡/" rel="tag"># 负载均衡</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/wnmp.html" rel="next" title="windows Nginx+PHP7+Mysql独立环境部署">
                <i class="fa fa-chevron-left"></i> windows Nginx+PHP7+Mysql独立环境部署
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/pdo_func.html" rel="prev" title="静态PDO基础操作封装">
                静态PDO基础操作封装 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://cdn.wm07.cn/pic.png"
                alt="空城" />
            
              <p class="site-author-name" itemprop="name">空城</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/phpxiaowei" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://segmentfault.com/u/jiachengxuyuan_59c0accf5a1ed" target="_blank" title="SegmentFault">
                    
                      <i class="fa fa-fw fa-meh-o"></i>SegmentFault</a>
                </span>
              
            
          </div>

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-globe"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://tianshengbufan.com" title="天生不凡博客" target="_blank" rel="external nofollow">天生不凡博客</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#准备镜像"><span class="nav-number">1.</span> <span class="nav-text">准备镜像</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#docker-compose容器编排"><span class="nav-number">2.</span> <span class="nav-text">docker-compose容器编排</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#安装Compose"><span class="nav-number">2.1.</span> <span class="nav-text">安装Compose</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用Compose启动多个Nginx容器"><span class="nav-number">3.</span> <span class="nav-text">使用Compose启动多个Nginx容器</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#创建编排脚本"><span class="nav-number">3.1.</span> <span class="nav-text">创建编排脚本</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配置Nginx"><span class="nav-number">4.</span> <span class="nav-text">配置Nginx</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#反向代理Nginx配置"><span class="nav-number">4.1.</span> <span class="nav-text">反向代理Nginx配置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#web应用Nginx配置"><span class="nav-number">4.2.</span> <span class="nav-text">web应用Nginx配置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#标记Nginx目录"><span class="nav-number">4.3.</span> <span class="nav-text">标记Nginx目录</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP环境安装"><span class="nav-number">5.</span> <span class="nav-text">PHP环境安装</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#创建自定义镜像"><span class="nav-number">5.1.</span> <span class="nav-text">创建自定义镜像</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#容器依赖"><span class="nav-number">6.</span> <span class="nav-text">容器依赖</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#最终编排脚本"><span class="nav-number">6.1.</span> <span class="nav-text">最终编排脚本</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#添加php配置文件"><span class="nav-number">6.2.</span> <span class="nav-text">添加php配置文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#修改web应用Nginx配置"><span class="nav-number">6.3.</span> <span class="nav-text">修改web应用Nginx配置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#重新编排容器"><span class="nav-number">6.4.</span> <span class="nav-text">重新编排容器</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#测试环境"><span class="nav-number">7.</span> <span class="nav-text">测试环境!</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#phpinfo"><span class="nav-number">7.1.</span> <span class="nav-text">phpinfo</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#session共享"><span class="nav-number">7.2.</span> <span class="nav-text">session共享</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">空城</span>

  
  <span class="post-meta-divider">|</span><a class="theme-link" target="_blank" href="http://www.miitbeian.gov.cn/" rel="external nofollow">湘ICP备17007213号</a>
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io" rel="external nofollow">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>




  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow">NexT.Gemini</a> v5.1.3</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'oDLKa1IlCi8XzHWzfdD4OCGS-gzGzoHsz',
        appKey: 'ggsl72lxBKGDysOTdpd06hBA',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("oDLKa1IlCi8XzHWzfdD4OCGS-gzGzoHsz", "ggsl72lxBKGDysOTdpd06hBA");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  

  

  

</body>
</html>
